$('#conuntlogin').click(function() {
    $('#bottom_log').css({ 'display': 'block' });
    $('#conuntlogin').addClass('active_list');
    $('#bottom_wechat').css({ 'display': 'none' });
    $('#wechatlogin').removeClass('active_list');
    $('#bottom_middle').css({ 'display': 'block' });
})
$('#wechatlogin').click(function() {
    $('#bottom_log').css({ 'display': 'none' });
    $('#conuntlogin').removeClass('active_list');
    $('#bottom_wechat').css({ 'display': 'block' });
    $('#wechatlogin').addClass('active_list');
    $('#bottom_middle').css({ 'display': 'none' });
})

// 如果登录过 自动填充
var account = localStorage.getItem('account');
if (account) {
    $('.phone').val(account);
    $('.phone').trigger('click');
}

//function count
//验证手机登录状态
var onoff = false;
$('#bottom_middle').click(function() {
    if(onoff) { return false }
    onoff = true;
    var inp = $('.phone').val();
    var sev = $('.severs').val();
    // 记录账号
    localStorage.setItem('account', inp);
    if(inp == '') {
        $('#bottom_tip').html('请输入手机号码');
        Tipanimate();
    } else if(!(/^1(2|3|4|5|6|7|8|9)\d{9}$/.test(inp))) {
        $('#bottom_tip').html('请输入正确手机号码');
        Tipanimate();
    } else if(sev == '') {
        $('#bottom_tip').html('请输入密码');
        Tipanimate();
    } else {
        $.ajax({
            type: "POST",
            url: apiCollect.apiLogin,
            data: {
                username: inp,
                password: sev
            },
            success: function(data) {
                if(data.code == '000000') {
                    if (data.data.token_key) {
                        localStorage.setItem('token', data.data.token_key);
                        localStorage.setItem('wid', data.data.wid);
                        window.location.href = '../index.html';
                        return false
                    }else{
                        localStorage.setItem('wid', data.data.wid);
                        $('#bottom_tip').html(data.message);
                        Tipanimate();
                        setTimeout(function () {
                            window.location.href = 'regesitorMain.html';
                        }, 1500);
                    }
                } else {
                    $('#bottom_tip').html(data.message);
                    Tipanimate()
                }
            }
        });
    }
	if(onoff) { return false }
	onoff = true;
	var inp = $('.phone').val();
	var sev = $('.severs').val();
	// 记录账号
	localStorage.setItem('account', inp);
	if(inp == '') {
		$('#bottom_tip').html('请输入手机号码');
		Tipanimate();
	} else if(!(/^1(2|3|4|5|6|7|8|9)\d{9}$/.test(inp))) {
		$('#bottom_tip').html('请输入正确手机号码');
		Tipanimate();
	} else if(sev == '') {
		$('#bottom_tip').html('请输入密码');
		Tipanimate();
	} else {
		$.ajax({
			type: "POST",
			url: apiCollect.apiLogin,
			data: {
				username: inp,
				password: sev
			},
			success: function(data) {
				if(data.code == '000000') {
					if (data.data.token_key) {
						localStorage.setItem('token', data.data.token_key);
						localStorage.setItem('wid', data.data.wid);
						window.location.href = '../index.html';
						return false
					}else{
						localStorage.setItem('wid', data.data.wid);
						$('#bottom_tip').html(data.message);
						Tipanimate();
						setTimeout(function () {
							window.location.href = 'regesitorMain.html';
						}, 1500);
					}
				} else {
					$('#bottom_tip').html(data.message);
					Tipanimate()
				}
			}
		});
	}
})

function Tipanimate() {
    $('#bottom_tip').animate({ 'height': '40px' }, 200);
    $('#bottom_tip').delay(1000).animate({ 'height': '0px' }, 200, function() {
        onoff = false;
        $('#bottom_tip').html('')
    });
}
$('#wechatlogin').click(function() {
    var obj = new WxLogin({
        id: "bottom_wechat",
        appid: "wx888d26b22f4a7f28",
        scope: "snsapi_login",
        redirect_uri: encodeURI("http://www.soundems.com/head_for/logIn/index.html"),
        state: '',
        style: "black",
        href: "https://www.quxianchang.com/css/oss.css"
    });
})

function getQuery(name) {
    var url = location.search;
    var reg = new Object();
    if(url.indexOf('?') != -1) {
        var str = url.substr(1);
        var strs = str.split('&');
        for(var i = 0; i < strs.length; i++) {
            reg[strs[i].split('=')[0]] = unescape(strs[i].split('=')[1]);
        }
    }
    return reg;
}
var code = getQuery('code').code;
if(code) {
    console.log(code)
    $.ajax({
        type: "POST",
        url: apiCollect.apiWXLogin,
        data: {
            wechatcode: code
        },
        success: function(data) {
            console.log(data)
            if(data.code == 000000) {
                if(data.data.wechat == 1) {
                    localStorage.setItem('wid', data.data.wid);
                    window.location.href = 'wxregistor.html?headimgurl='+data.data.headimgurl
                } else if(data.data.wechat == 2) {
                    window.location.href = 'regesitorMain.html'
                } else if(data.data.wechat == 0) {
                    localStorage.setItem('token', data.data.token_key);
                    localStorage.setItem('wid', data.data.wid);
                    window.location.href = '../index.html'
                }
            } else {
                console.log(data.message)
            }
        }
    });
} else {
    $('#page-loading').hide();
}

$(document).on('keydown', function (ev) {
    if (ev.keyCode == 13) {
        $('#bottom_middle').trigger('click');
    }
});
